<template>
  <base-modal
    :show.sync="visible"
    :title="title"
    width="500px"
    @close="visible = false"
  >
    <div class="pad-20">
      <div class="mar-b-20">{{$t('serviceProvider.refuseProviderJoin')}}</div>
      <el-form label-position="top" ref="form" :model="form" :rules="rules">
        <el-form-item label="" required prop="reason">
          <el-input
            v-model="form.reason"
            type="textarea"
            maxlength="500"
            rows="5"
            :placeholder="$t('common.validate.inputRefuseReason')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="plain-third" @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="validate" :disabled="pending"
        >{{$t('common.base.confirm')}}</el-button
      >
    </div>
  </base-modal>
</template>

<script>
/* api */
import { approveRegisterProvider } from '@src/api/serviceProviderApi';
export default {
  name: 'AuditReject',
  data() {
    return {
      pending: false,
      title: this.$t('common.base.refuse'),
      visible: false,
      form: {
        reason: '',
      },
      rules: {
        reason: [
          {
            required: true,
            message: this.$t('common.validate.inputRefuseReason'),
            trigger: ['blur', 'change'],
          },
        ],
      },
      id: '',
    };
  },
  methods: {
    openDialog(id) {
      this.$refs?.form?.clearValidate();
      this.form = {
        reason: '',
      };
      this.visible = true;
      this.id = id;
    },

    validate() {
      try {
        this.pending = true;
        this.$refs.form.validate(async vaild => {
          if (vaild) {
            await this.rest();
          }
        });
      } catch (e) {
        console.error(e);
      } finally {
        this.pending = false;
      }
    },
    async rest() {
      let data = {
        id: this.id,
        rejectReason: this.form?.reason,
        approveStatus: 2,
      };
      try {
        let res = await approveRegisterProvider(data);
        this.$message({
          type: res.success ? 'success' : 'error',
          message: res.success ? this.$t('common.validate.refuseSuccess') : res.message,
          showClose: true,
          duration: 2000,
        });
        this.$emit('refresh');
      } catch (e) {
        console.error(e);
      } finally {
        this.visible = false;
      }
    },
  },
};
</script>

<style scoped></style>
